package com.online.edu.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.online.edu.course.entity.File;
import com.online.edu.course.entity.Chapter;
import com.online.edu.course.entity.ChapterPeriod;
import com.online.edu.course.entity.PeriodFileMapping;
import com.online.edu.course.mapper.ChapterPeriodMapper;
import com.online.edu.course.service.IChapterPeriodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.edu.course.utils.FileStoreUtil;
import com.online.edu.course.utils.FileUtil;
import com.online.edu.util.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 * 课时表 服务实现类
 * </p>
 *
 * @author world
 * @since 2021-07-30
 */
@Service
public class ChapterPeriodServiceImpl extends ServiceImpl<ChapterPeriodMapper, ChapterPeriod> implements IChapterPeriodService {

    @Autowired
    BaseMapper<File> filebaseMapper;

    @Autowired
    BaseMapper<Chapter> chapterBaseMapper;

    @Autowired
    BaseMapper<ChapterPeriod> cpBaseMapper;

    @Autowired
    BaseMapper<PeriodFileMapping> pfBaseMapper;

    @Autowired
    FileStoreUtil client;

    @Autowired
    FileUtil fileUtil;

    //新增课时与章节列表绑定
    @Override
    public void addPeriodFileMapping(long periodId, long fileId){
        PeriodFileMapping periodFileMapping = new PeriodFileMapping();
        periodFileMapping.setPeriodFileId(Long.valueOf(StringUtil.uuid()));
        periodFileMapping.setPeriodId(periodId);
        periodFileMapping.setFileId(fileId);
        pfBaseMapper.insert(periodFileMapping);
    }

    //新增课时表
    @Override
    public ChapterPeriod addChapterPeriod(ChapterPeriod chapterPeriod,long chapterId) {
        //ChapterPeriod period = new ChapterPeriod();
        cpBaseMapper.insert(chapterPeriod);
        Long courseId = chapterBaseMapper.selectById(chapterId).getCourseId();
        UpdateWrapper<ChapterPeriod> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("id",Long.valueOf(StringUtil.uuid()))
                .set("course_id",courseId)
                .set("chapter_id",chapterId);
        cpBaseMapper.update(chapterPeriod,updateWrapper);
        return chapterPeriod;
    };

    /**
     * 根据上传的文件返回上传文件的信息
     * @param multipartFile 目标文件
     * @return 转储数据库的字段
     */
    //存储文件
    @Override
    public File saveFile(MultipartFile multipartFile) {
        File file = new File();
        //获取上传文件名称中.的位置
        int index = multipartFile.getOriginalFilename().indexOf(".");
        //获取上传文件名的长度
        int length = multipartFile.getOriginalFilename().length();
        file.setId(Long.valueOf(StringUtil.uuid()));
        file.setFileNo(1L);
        file.setFileName(multipartFile.getOriginalFilename().substring(0,index));
        //调用uploadFile获取上传文件返回的url
        file.setFileUrl(uploadFile(multipartFile));
        //获取文件类型分类存储
        String fileType = multipartFile.getOriginalFilename().substring(index+1, length);
        System.out.println(fileType);
        if ( fileType.equals("mp4") || fileType.equals("avi") || fileType.equals("rmvb")){
            file.setFileType(3);
            //如果文件为视频，则插入时长，单位s
            try {
                file.setVedioDuration(fileUtil.getFileTime(multipartFile));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if ( fileType.equals("png") || fileType.equals("jpg")){
            file.setFileType(2);
        }
        else {
            file.setFileType(1);
        }
        //获取文件大小
        file.setFileSize(multipartFile.getSize());
        filebaseMapper.insert(file);
        return file;
    }

    //上传文件
    private String uploadFile(MultipartFile file){
        //设置文件上传前面的拼接地址
        String url = "http://121.43.108.189:8080/";

        try {
            InputStream inputStream = file.getInputStream();
            String extName = StringUtil.getFileExtName(file.getOriginalFilename());
            String uploadPath = client.uploadFile(inputStream, extName);
            url = url + uploadPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }
}
